package cn.antho.superhelp.service.impl;

import cn.antho.superhelp.domain.Order;
import cn.antho.superhelp.domain.Reservation;
import cn.antho.superhelp.domain.Staff;
import cn.antho.superhelp.dto.ConstructionDto;
import cn.antho.superhelp.dto.DistributeStaffDto;
import cn.antho.superhelp.dto.ReservationTimeDto;
import cn.antho.superhelp.exception.GlobalExceptionCodeEnum;
import cn.antho.superhelp.feign.StaffService;
import cn.antho.superhelp.mapper.OrderMapper;
import cn.antho.superhelp.result.JsonResult;
import cn.antho.superhelp.service.IOrderService;
import cn.antho.superhelp.utils.AssertUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author director
 * @since 2022-12-12
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private StaffService staffService;

    /**
     * 分配施工团队
     * @param distributeStaffDto
     * @return
     */
    @Override
    public JsonResult distributeConstructionTeam(DistributeStaffDto distributeStaffDto) {
        String orderNo = distributeStaffDto.getNo();
        Long staffId = distributeStaffDto.getStaffId();
        // 参数校验
        // 业务校验
        // 1.校验员工是否存在
        JsonResult jsonResult = staffService.selectOne(staffId);
        AssertUtil.isTrue(jsonResult.isSuccess(), GlobalExceptionCodeEnum.ERROR);
        Object data = jsonResult.getData();
        Staff staff = JSONObject.parseObject(JSON.toJSONString(data), Staff.class);
        AssertUtil.isNotNull(staff, GlobalExceptionCodeEnum.ERROR);
        // 2.校验订单是否存在
        Order order = selectByOrderNo(orderNo);
        AssertUtil.isNotNull(order, GlobalExceptionCodeEnum.ERROR);
        // 3.分配施工队
        order.setConstructionTeam(staffId);
        order.setStatusOrder(Order.NO_APPOINTMENT_FOR_CONSTRUCTION);
        updateById(order);
        return JsonResult.success();
    }

    /**
     * 预约施工时间
     * @param reservationTimeDto
     * @return
     */
    @Override
    public JsonResult reservationTime(ReservationTimeDto reservationTimeDto) {
        Date date = new Date();
        String orderNo = reservationTimeDto.getNo();
        Date reservationTime = reservationTimeDto.getReservationTime();
        // 参数校验
        // 业务校验
        boolean boo = reservationTime.after(date);
        AssertUtil.isTrue(boo, GlobalExceptionCodeEnum.ERROR);
        // 预定时间
        Order order = selectByOrderNo(orderNo);
        order.setAppointmentTime(reservationTime);
        order.setStatusOrder(Order.RESERVED_FOR_CONSTRUCTION);
        updateById(order);
        return JsonResult.success();
    }

    @Override
    public JsonResult construction(ConstructionDto constructionDto) {
        // 参数校验
        Date date = new Date();
        String orderNo = constructionDto.getOrderNo();
        String pics = constructionDto.getPics();
        // 业务校验
        Order order = selectByOrderNo(orderNo);
        order.setPics(pics);
        order.setRealAppointmentTime(date);
        order.setStatusOrder(Order.COMPLETION_OF_CONSTRUCTION);
        updateById(order);
        // TODO 调用支付服务
        return JsonResult.success();
    }


    private Order selectByOrderNo(String orderNo) {
        Wrapper<Order> wrapper = new EntityWrapper<>();
        wrapper.eq("order_no", orderNo);
        return selectOne(wrapper);
    }
}
